<!DOCTYPE HTML>
<html>
<?php session_start();?>
	<head>
		<meta charset="utf-8">
		<title>订单后台</title>
	</head>
	<body>
		<center>
		<?PHP
			date_default_timezone_set("PRC");

			include("../../public/config.php");
			$link = mysqli_connect(HOST,USER,PASS,DBNAME) or die("数据连接失败！");
			mysqli_set_charset($link,"utf8");
			switch ($_GET['a']) {							//根据a的传值进行对应的操作
				case 'add':
					$address = $_POST['address'];			//接收用户输入的收货地址等信息
					$phone = $_POST['phone'];				
					$code = $_POST['code'];
					$linkman = $_POST['linkman'];
					$total = $_POST['total'];
					$uid = $_SESSION[adminuser][id];
					$addtime = time();
					$status = 0;		
					//拼接sql语句
				    $sql = "insert into orders(uid,linkman,address,code,phone,addtime,total,status) 
							values ('{$uid}','{$linkman}','{$address}','{$code}','{$phone}','{$addtime}','{$total}',{$status}) ";						
					mysqli_query($link,$sql);
					$rowID=mysqli_insert_id($link);					//写入数据库
					if(mysqli_insert_id($link)<0){					//判断写入数据库是否成功
						echo "系统崩溃啦~~~下单失败!请重新下单";
						header("refresh:2;url=order.php");			//跳转
						exit();
					}
					// echo $rowID;
					if(!empty($_SESSION['shoplist'])){						//从session中获取数据
						foreach ($_SESSION['shoplist'] as $value) {
							//把购物车里面的数据写入数据库
							$sql2 = "insert into detail(orderid,goodsid,name,price,num) values ('{$rowID}','{$value['id']}','{$value['goods']}','{$value['price']}','{$value['num']}')";
							// echo "$sql2";
							mysqli_query($link,$sql2);						//执行sql语句
							
						}
						unset($_SESSION['shoplist']);						//销毁购物车的session
					}else{			//判断购物车是否有商品
						echo '<script>alert("购物车还没东西哦~~~确定后返回！"); </script>';
						header("refresh:0;url=../index.php");
						exit();
					}
						// exit();
						echo '<script>alert("下单成功!！"); </script>';	//下单成功后进行跳转
						header("refresh:0;url=../index.php");
						exit();
						break;
				
				case 'confirm':									//对订单进行操作
					$id = $_GET['id'];
					$sql = "select * from orders where id='{$id}'";	//判断是不是无效订单
					$res = mysqli_query($link,$sql);				
					$con = mysqli_fetch_assoc($res);

					if($con['status']==3){						//如果是无效订单则不允许确认收货
						echo '<script>alert("无效订单，不能再确认收货！"); </script>';
						header("refresh:0;url=index.php");
						exit();
					}

					//判断是不是新订单
					if($con['status']==0){
						echo '<script>alert("商家还没发货，不能确认收货！"); </script>';
						header("refresh:0;url=index.php");
						exit();
					}

					//确定收货
					$sql = "update orders set status='2' where id={$id}";
					mysqli_query($link,$sql);
					if(mysqli_affected_rows($link)>0){
						header("Location:".$_SERVER["HTTP_REFERER"]);	//跳转返回
						exit();
					}else{
						echo '<script>alert("已确认！"); </script>';
						header("refresh:0;url=index.php");
						exit();
					}
					break;

				case 'cancel':					//取消订单操作
					$id = $_GET['id'];
					$sql = "select * from orders where id='{$id}'";	//判断是不是无效订单
					$res = mysqli_query($link,$sql);				
					$con = mysqli_fetch_assoc($res);

					if($con[status]!=0){						//如果是无效订单则不允许确认收货
						echo '<script>alert("不能取消订单！"); </script>';
						header("refresh:0;url=index.php");
						exit();
					}


					$sql = "update orders set status='3' where id={$id}";
					mysqli_query($link,$sql);
					if(mysqli_affected_rows($link)>0){
						header("Location:".$_SERVER["HTTP_REFERER"]);
						exit();
					}else{
						echo '<script>alert("订单已取消！"); </script>';
						header("refresh:0;url=index.php");
						exit();
					}
					break;


					//商品评价
				case 'pingjia':
					$goodsid = $_POST['id'];
					$uid = $_SESSION['adminuser']['id'];
					$connect = $_POST['connect'];
					$connect = rtrim($connect);
					$addtime = time();
					if (empty($connect)) {
						echo '<script>alert("评价内容不能为空！"); </script>';
						header("refresh:0;url=pingjia.php?id={$goodsid}");
						exit();
					}
					$sql = "insert into comment(goodsid,uid,connect,addtime) 
							values('$goodsid','$uid','$connect','$addtime')";
					mysqli_query($link,$sql);
					if (mysqli_insert_id($link)>0) {
						echo '<script>alert("发布成功！"); </script>';
						header("refresh:0;url=index.php");
						exit();
					}else{
						echo '<script>alert("发布失败！"); </script>';
						header("refresh:5;url=index.php");
						exit();
					}
					break;


					//删除操作
				case 'del':
					$id = $_GET['id'];
					$sql = "delete  from comment where id={$id}";
					mysqli_query($link,$sql);
					if (mysqli_affected_rows($link)) {
						echo '<script>alert("删除成功！"); </script>';
						header("refresh:0;url=mypingjia.php");
						exit();
					}else{
						echo '<script>alert("删除失败！"); </script>';
						header("refresh:0;url=mypingjia.php");
						exit();
					}
					break;


				case 'xiugai':
					$id = $_POST['id'];
					$connect = $_POST['connect'];
					$connect = rtrim($connect);
					if (empty($connect)) {
						echo '<script>alert("评价内容不能为空！"); </script>';
						header("refresh:0;url=mypingjia.php");
						exit();
					}
					$sql = "update comment set connect='{$connect}' where id='{$id}'";
					mysqli_query($link,$sql);
					if (mysqli_affected_rows($link)) {
						echo '<script>alert("修改成功！"); </script>';
						header("refresh:0;url=mypingjia.php");
						exit();
					}else{
						echo '<script>alert("修改失败！"); </script>';
						header("refresh:0;url=mypingjia.php");
						exit();
					}

					break;


			}
		?>
		</center>
	</body>
</html>